//
//  ESDuanziTableCell.m
//  Eyeshot
//
//  Created by admin on 15/7/3.
//  Copyright (c) 2015年 eyeshot. All rights reserved.
//

#define kMargin (2 * kHorizen)
#define kFontSize 15
#define kTopViewHeight (12 * kVertical)
#define kButtomViewHeight (8 * kVertical)
#import "ESDuanziTableCell.h"
#import "ESCellTopView.h"
#import "ESDuanzi.h"
#import "ESToolbar.h"



@interface ESDuanziTableCell ()
@property (nonatomic, weak) ESCellTopView *topView;
@property (nonatomic, weak) UILabel *fragmentLabel;
@property (nonatomic, weak) ESToolbar *buttomView;
@property (nonatomic, weak) UIView *baseView;
@end

@implementation ESDuanziTableCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        UIView *baseView = [[UIView alloc] initWithFrame:CGRectMake(kMargin, kMargin, SCREEN_WIDTH - 2 * kMargin,  kTopViewHeight)];
        baseView.backgroundColor = [UIColor whiteColor];
        
        ESCellTopView *topView = [ESCellTopView cellTopView];
        topView.frame = CGRectMake(kMargin, kMargin, SCREEN_WIDTH - 2 * kMargin,  kTopViewHeight);
        topView.backgroundColor = [UIColor whiteColor];
        
        UILabel *fragmentLabel = [[UILabel alloc] init];
        fragmentLabel.frame = CGRectMake(2 * kMargin, kMargin +  kTopViewHeight + 2 * kVertical, SCREEN_WIDTH - 4 * kMargin, kVertical);
        fragmentLabel.numberOfLines = 0;
        fragmentLabel.font = [UIFont systemFontOfSize:kFontSize];
        fragmentLabel.backgroundColor = [UIColor whiteColor];
        
        ESToolbar *buttomView = [ESToolbar cellButtomView];
        buttomView.frame = CGRectMake(kMargin, kMargin +  kTopViewHeight + 2 * kVertical, SCREEN_WIDTH - 2 * kMargin,  kButtomViewHeight);
        buttomView.backgroundColor = [UIColor whiteColor];
        
        [self.contentView addSubview:baseView];
        [self.contentView addSubview:topView];
        [self.contentView addSubview:fragmentLabel];
        [self.contentView addSubview:buttomView];
        
        _baseView = baseView;
        _topView = topView;
        _fragmentLabel = fragmentLabel;
        _buttomView = buttomView;
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.contentView.backgroundColor = ESColor(234, 234, 234);
    }
    return self;
    
}

+ (CGFloat)heightForFragmentLabelWithModel:(ESDuanzi *)model {
    
    NSDictionary *dict = @{NSFontAttributeName: [UIFont systemFontOfSize:kFontSize]};
    CGFloat fragmentH = [model.text boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 4 * kMargin, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size.height;
    
    return fragmentH;
}

+ (CGFloat)heightForCellWithModel:(ESDuanzi *)model {
    
    CGFloat topViewH =  kTopViewHeight;
    CGFloat fragmentH = [self heightForFragmentLabelWithModel:model];
    CGFloat buttomH =  kButtomViewHeight;
    
    return kMargin + topViewH + 2 * kVertical + fragmentH + 2 * kVertical + buttomH;
}

- (void)setModel:(ESDuanzi *)model {
    CGFloat topViewH =  kTopViewHeight;
    CGFloat fragmentH = [ESDuanziTableCell heightForFragmentLabelWithModel:model];
    CGFloat buttomH =  kButtomViewHeight;
    _fragmentLabel.height = fragmentH;
    _buttomView.y = kMargin + topViewH + 2 * kVertical + fragmentH + 2 * kVertical;
    _baseView.height = topViewH + 2 * kVertical + fragmentH + 2 * kVertical + buttomH;

    _fragmentLabel.text = model.text;
    
    [_topView.userProfile sd_setImageWithURL:[NSURL URLWithString:model.profile_image] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"default64"]];
    [_topView.userProfile setTitle:model.screen_name forState:UIControlStateNormal];
    _topView.userProfile.subLabel.text = model.passtime;
    
    //根据模型给button的title赋值
    [self setButtonWithTitle:model.love type:ESToolbarBtnTypeDing];
    [self setButtonWithTitle:model.hate type:ESToolbarBtnTypeCai];
    [self setButtonWithTitle:model.repost type:ESToolbarBtnTypeShare];
    [self setButtonWithTitle:model.comment type:ESToolbarBtnTypeComment];
    
    [_topView.userProfile setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    _topView.userProfile.titleLabel.font = [UIFont systemFontOfSize:12];
    _topView.userProfile.titleLabel.textAlignment = NSTextAlignmentLeft;
    _topView.userProfile.subLabel.textColor = [UIColor grayColor];
    _topView.userProfile.subLabel.font = [UIFont systemFontOfSize:10];
}

//判断title是否为0,如果为零则不设置
- (void)setButtonWithTitle:(NSString *)title type:(ESToolbarBtnType)type
{
    if (![title isEqualToString:@"0"])
    {
        UIButton *btn = (UIButton *)[self.buttomView viewWithTag:type];
        [btn setTitle:title forState:(UIControlStateNormal)];
    }
}

@end
